package com.guoguo.service.impl;

import com.github.pagehelper.PageHelper;
import com.guoguo.base.PageBean;
import com.guoguo.base.PaginationContext;
import com.guoguo.entity.Images;
import com.guoguo.entity.Statistical;
import com.guoguo.entity.vo.AcAlbumImageVO;
import com.guoguo.entity.vo.ImagesVO;
import com.guoguo.entity.vo.RankingVO;
import com.guoguo.enums.AliOssImageEnum;
import com.guoguo.mapper.AlbumMapper;
import com.guoguo.mapper.ImagesMapper;
import com.guoguo.mapper.StatisticalMapper;
import com.guoguo.service.IStatisticalService;
import com.guoguo.utils.*;
import com.guoguo.utils.aliOss.AliOssImageConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * Created by Administrator on 2017/8/31 0031.
 */

     /*
                           _ooOoo_
                          o8888888o
                          88" . "88
                          (| -_- |)
                          O\  =  /O
                       ____/`---'\____
                     .'  \\|     |//  `.
                    /  \\|||  :  |||//  \
                   /  _||||| -:- |||||-  \
                   |   | \\\  -  /// |   |
                   | \_|  ''\---/''  |   |
                   \  .-\__  `-`  ___/-. /
                 ___`. .'  /--.--\  `. . __
              ."" '<  `.___\_<|>_/___.'  >'"".
             | | :  `- \`.;`\ _ /`;.`/ - ` : | |
             \  \ `-.   \_ __\ /__ _/   .-` /  /
        ======`-.____`-.___\_____/___.-`____.-'======
                           `=---='
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
                 佛祖保佑       永无BUG
        */
@Service
public class StatisticalServiceImpl implements IStatisticalService {
    private static Logger log = LoggerFactory.getLogger(StatisticalServiceImpl.class);//日志

    @Autowired
    private StatisticalMapper statisticalMapper;

    @Autowired
    private AlbumMapper albumMapper;

    @Autowired
    private ImagesMapper imagesMapper;

    @Override
    public List<RankingVO> getRanking(int type) {
        List<RankingVO> rankingVOs = statisticalMapper.getRanking(type);
        log.info("========>>>>>>>  ranking size : " + rankingVOs.size());

//        List<AliOssImageConfig> aliOssImageConfigsList = new ArrayList<>();
//        AliOssImageConfig aliOssImageConfig = new AliOssImageConfig("test");
//        aliOssImageConfigsList.add(aliOssImageConfig);

        for (RankingVO rankingVO : rankingVOs) {

            List<Images> imagesList = new ArrayList<>();
            List<ImagesVO> imagesVOs = imagesMapper.getAlbumImageVOList(rankingVO.getAlbId());
            rankingVO.setModelIcon(AliOssUtils.getImageUrl(rankingVO.getModelIcon()));//, aliOssImageConfig));
            for (ImagesVO imagesVO : imagesVOs) {
                //将文件路径换成可以访问的url
                List<String> files = new ArrayList<>();
                files.add(imagesVO.getOriginalUrl());
                files.add(imagesVO.getCompressionUrl());
                files.add(imagesVO.getThumbnailsUrl());
                Map<String, String> urlMap = AliOssUtils.getImageUrlList(files);//, aliOssImageConfigsList);
                imagesVO.setOriginalUrl(urlMap.get(imagesVO.getOriginalUrl()));
                imagesVO.setCompressionUrl(urlMap.get(imagesVO.getCompressionUrl()));
                imagesVO.setThumbnailsUrl(urlMap.get(imagesVO.getThumbnailsUrl()));

                Images images = (Images) ClazzUtils.copyBean(imagesVO, new Images());
                imagesList.add(images);
            }
            rankingVO.setAlbumImagesVO(imagesList);
        }
        return rankingVOs;
    }

    @Override
    public void CountRanking() {

        int num = statisticalMapper.removeRanking(DateUtils.getDateByFormat(new Date(), "yyyy-MM-dd"));

        if (num > 0) {
            log.info("===================>>>>>>         今天已经统计过，已删除统计记录【 " + num + " 】 条！");
        } else {
            log.info("===================>>>>>>         今天还没有统计，即将进行统计今天【 " + DateUtils.getDateByFormat(new Date(), "yyyy-MM-dd") + " 】的数据！");
        }

        List<Map<String, Object>> hotList = albumMapper.getFireHotRanking();
        saveRanking(hotList, 1);

        List<Map<String, Object>> payList = albumMapper.getPayRanking();
        saveRanking(payList, 2);

        List<Map<String, Object>> likeList = albumMapper.getLikeRanking();
        saveRanking(likeList, 3);
    }

    @Override
    public RankingVO selectByIdType(String albId, int type) {
        return statisticalMapper.selectByIdType(albId, type);

    }

    @Override
    public int updateByIdType(Statistical statistical) {
        return statisticalMapper.updateByIdType(statistical);
    }

    private void saveRanking(List<Map<String, Object>> maps, int type) {
        int num = 0;
        for (Map<String, Object> stringObjectMap : maps) {
            Statistical statistical = new Statistical();
            statistical.setId(UUIDUtils.getUUID());
            statistical.setType(type);
            statistical.setNum(Integer.valueOf(String.valueOf(stringObjectMap.get("num"))));
            statistical.setAlbId(String.valueOf(stringObjectMap.get("alb_id")));
            statistical.setRankTime(new Date());

            num += statisticalMapper.addRanking(statistical);
        }
    }
}
